﻿package view
{
	import controller.GameController;
	
	import fl.lang.Locale;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.sampler.NewObjectSample;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
		
		
	public class Introduction
	{
		private var movie:GameController;
		private var cart:Cart = new Cart();			
		private var currentExplanation:Sound;
		private var currentSoundChannel:SoundChannel;
		private var nextExplanation:Function;
		private var skipBtn:ForwardButton = new ForwardButton();
		private var repeatBtn:RepeatBtn = new RepeatBtn();
		private var buttons:MovieClip = new MovieClip();
		private var shelf:Shelf;
		private var transMan:TransitionManager;
		private var transition:Transition;		
		private var skipped = true;
		private var gameStartCallback:Function;
		private var keyboard = new Keyboard();
		
		public function Introduction(mov:GameController, gameStartCallback) {
			this.movie = mov;
			this.gameStartCallback = gameStartCallback;
			
			//hide playing cart
			this.movie.cart_front.visible = false;
			this.movie.cart_back.visible = false;

			this.movie.addChildAt(this.cart, 15);			
			
			this.shelf = new Shelf(this.movie.xml.getQuestionToLevel(1).getProducts());
			this.shelf.visible = false;
			this.movie.addChildAt(this.shelf, Config.ShelfIndexZ);	
			
			this.initKeyboard();			
			this.initBtns();
		}
		
		private function initBtns() {
			this.skipBtn.addEventListener(MouseEvent.CLICK, this.skipIntro);
			this.repeatBtn.addEventListener(
				MouseEvent.CLICK,
				function (e:Event) {
					stopIntro(e);
					cart.moveCartFromCurrentXTo(
						0,
						function (e:Event) {
							movie.visible = false;
							fadeIn();  //array mit timern, alle durchgehen und stoppen
						},
						true
					);
				}
			);
			this.buttons.addChild(this.repeatBtn);
			this.buttons.addChild(this.skipBtn);	
		}
		
		private function initKeyboard():void 
		{
			this.keyboard.x = 313;
			this.keyboard.y = 150;
			this.keyboard.visible = false;
			this.movie.addChild(this.keyboard);	
		}
		
		private function loadExplanation(file:String):void
		{
			this.currentExplanation = new Sound(new URLRequest(Config.IntroSoundPath + file + ".mp3"));
		}		
		
		public function playExplanation():void
		{
			this.currentSoundChannel = this.currentExplanation.play();
			this.cart.startFaceMoving(this.currentSoundChannel);	
			this.currentSoundChannel.addEventListener(Event.SOUND_COMPLETE, cart.stopFaceMoving);						
			//start following method which should be called after sound play
			this.currentSoundChannel.addEventListener(Event.SOUND_COMPLETE, this.nextExplanation);
		}		
		
		public function fadeIn() {
			//set following method to call
			this.nextExplanation = this.introduceGamers;

			//fading transiont whichs starts bringing in of cart after finishing
			this.transMan = new TransitionManager(this.movie);
			this.transMan.addEventListener("allTransitionsInDone",
				function bringUpCart(e:Event) {
					cart.moveCartFromCurrentXTo(Config.cartSpeakingPosition, nextExplanation, true);
					
				}
			);
			this.transition = this.transMan.startTransition({type:Fade, direction:Transition.IN, duration:2, easing:None.easeOut});
			//this.transMan.startTransition({type:Blinds, direction:Transition.IN, duration:3, easing:None.easeOut, numStrips:25, dimension:1});		
			//this.transMan.startTransition({type:PixelDissolve, direction:Transition.IN, duration:2, easing:None.easeOut, xSections:30, ySections:20});
		}
		
		private function introduceGamers(e:Event)
		{
			//reset skipped flag, used for highlight timer @see this.highlightItem()
			this.skipped = false;							
			//make btns invisible and add them to stage
			this.buttons.visible = false;
			this.movie.addChildAt(this.buttons, this.movie.numChildren - 1);
			
			//load sound for introducing and set following method to call after finishing introducing
			this.loadExplanation("introduceGamers");
			this.nextExplanation = this.explainForwardBtn;

			//a transition that fads in the buttons and starts the sound play after finishing
			this.transMan = new TransitionManager(this.buttons);
			this.transMan.addEventListener("allTransitionsInDone", function (e:Event) { playExplanation() } );
			this.transition = this.transMan.startTransition({type:Fade, direction:Transition.IN, duration:1, easing:None.easeOut});			
		}
		
		public function explainForwardBtn(e:Event)
		{
			this.loadExplanation("explainForwardBtn"); //load current explanation
			this.nextExplanation = this.explainShelf; // set next method that should be called after sound playing
			this.playExplanation();	//play sound
		}	
		
		public function explainShelf(e:Event)
		{		
			this.transMan = new TransitionManager(this.shelf);
			this.transMan.addEventListener("allTransitionsInDone", this.explainConveyor);			
			this.highlightItem(
				this.skipBtn, 
				function () {
					transition = transMan.startTransition({type:Fade, direction:Transition.IN, duration:2, easing:None.easeOut});
				}
			);
		}
		
		public function explainConveyor(e:Event)
		{
			this.loadExplanation("explainConveyor");
			this.nextExplanation = this.explainControlling;					
			this.playExplanation();
		}		
		
		public function explainControlling(e:Event)
		{
			this.loadExplanation("explainControlling");
			this.nextExplanation = this.explainControlLeft;				
			
			this.playExplanation();
		}	
		
		public function explainControlLeft(e:Event)
		{			
			this.loadExplanation("explainControlLeft");
			this.nextExplanation = this.explainControlRight;	

			this.transMan = new TransitionManager(this.keyboard);
			this.transMan.addEventListener(
				"allTransitionsInDone",
				function (e:Event) {
					playExplanation();
				}
			);			
			this.transition = transMan.startTransition({type:Fade, direction:Transition.IN, duration:2, easing:None.easeOut});			
		}	
		
		public function explainControlRight(e:Event)
		{
			this.loadExplanation("explainControlRight");
			this.nextExplanation = this.explainCatchPoints;
			this.highlightItem(this.keyboard.keyLeft, this.playExplanation);
		}			
		
		public function explainCatchPoints(e:Event)
		{			
			this.loadExplanation("explainCatchPoints");
			this.nextExplanation = this.explainHelpBtn;
	
			this.highlightItem(
				this.keyboard.keyRight,
				function () {	
					keyboard.visible = false;
					playExplanation();
				}
			);
		}	

		public function explainHelpBtn(e:Event)
		{
			
			this.loadExplanation("explainHelpBtn");
			this.nextExplanation = this.explainStars;
			this.highlightItem(this.movie.wagen1);			
			this.highlightItem(this.movie.wagen2);			
			this.highlightItem(this.movie.wagen3);
			this.highlightItem(this.movie.wagen4);			
			this.highlightItem(this.movie.wagen5, this.playExplanation);			
		}		
		
		public function explainStars(e:Event)
		{			
			this.loadExplanation("explainStars");
			this.nextExplanation = this.explainStarsEnd;
			this.highlightItem(this.movie.helpBtn, this.playExplanation);					
		}		
		
		public function explainStarsEnd(e:Event)
		{
			this.loadExplanation("explainStarsEnd");
			this.nextExplanation = this.skipIntro;			
			this.highlightItem(this.movie.textField);			
			this.highlightItem(this.movie.starsSymbol, this.playExplanation);					
		}
		
		public function finishExplanation(e:Event)
		{}
		
		/* missing sounds	

		public function explainCart(e:Event)
		{				
		}			
		
		public function explainSmilies(e:Event)
		{
			this.loadExplanation(Config.ExplanationSoundPath + "tmp.mp3");
			this.nextExplanation = this.explainStars;
			this.highlightItem(this.movie.life1);			
			this.highlightItem(this.movie.life2);			
			this.highlightItem(this.movie.life3, this.playExplanation);							
		}						
		
		public function explainRepeatBtn(e:Event)
		{
			this.loadExplanation(Config.ExplanationSoundPath + "tmp.mp3");
			this.nextExplanation = this.explainRepeatBtn;		
			this.highlightItem(this.skipBtn, this.playExplanation);		 	
		}	
		
		*/

		
		public function exitIntro(e:Event){
			(this.movie.contains(this.shelf)) ? this.movie.removeChild(this.shelf) : null ;							
			this.movie.removeChild(this.cart);	
			this.movie.removeChild(this.keyboard);
			this.movie.cart_front.visible = true;
			this.movie.cart_back.visible = true;
			this.gameStartCallback();	
		}
		
		public function skipIntro(e:Event) {
			this.skipped = true;
			this.stopIntro(e);
			this.cart.moveCartFromCurrentXTo(
				-9.3 ,
				function (e:Event) {
					exitIntro(e);
				},
				true
			);			
		}
		
		public function stopIntro(e:Event) 
		{			
			this.skipped = true;
			if (this.currentSoundChannel) {
				/* wird nicht benoetigt, Event feuert nicht bei stop()
				this.currentSoundChannel.removeEventListener(
				Event.SOUND_COMPLETE,
				this.currentEventListener
				);		
				*/
				this.cart.stopFaceMoving(e);
				this.currentSoundChannel.stop();
			}
			(this.transition) ? this.transition.stop() : null;
			this.shelf.visible = false;
			this.keyboard.visible = false;
			this.movie.removeChild(this.buttons);
		}
		
		private function highlightItem(obj:DisplayObject, startWhenFinished:Function = null) {
			var changeTik:int = 80;
			var alphaStep:Number = 0.05;
			var targetAlpha:Number = 0.6;
			var state:String = 'in';
			
			var glowTimer:Timer = new Timer(changeTik);
			var newAlpha:Number = 0.0;
			glowTimer.addEventListener(
				TimerEvent.TIMER,
				function (e:Event) {
					if (skipped) {
						e.currentTarget.stop();
						return;
					}
					if (state == 'in') {
						if (newAlpha >= targetAlpha) {
							state = 'out';
						} else {
							newAlpha += alphaStep;	
						}				
					} else if (state == 'out') {
						if (newAlpha <= 0.0) {
							e.currentTarget.stop();
							if (startWhenFinished != null) {
								startWhenFinished();
							}							
							return;
						} else {
							newAlpha -= alphaStep;	
						}	
					}
					obj.filters = [new GlowFilter(0xFFFFFF, newAlpha, 25, 25, 5, 2, false, false)];
				}
			);
			if (!this.skipped) {
				glowTimer.start();
			}			
		}
	}
}